package edu.scut.course_management.po;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 持久化对象：教师
 * @author sherry
 */
public class Teacher implements UserDetails {
    
    private static final long serialVersionUID = -2640905646899189785L;
    private Integer roleId;
    @JsonIgnore
    List<Role> roles;  // 用户权限列表
    private String teacherId;
    private String password;
    private String name;
    private boolean male;
    private String phoneNumber;
    private Integer departmentId;
    private String academicTitle;

    @Override
    public String getUsername() {
        return getTeacherId();
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
    
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return getRoles();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(String teacherId) {
        this.teacherId = teacherId;
    }

    @Override
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isMale() {
        return male;
    }

    public void setMale(boolean male) {
        this.male = male;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Integer getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }

    public String getAcademicTitle() {
        return academicTitle;
    }

    public void setAcademicTitle(String academicTitle) {
        this.academicTitle = academicTitle;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(Role role) {
        this.roles.add(0, role);
    }

    // 生成基本账号信息
    public Map<String, Object> generateBasicInfoMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", teacherId);
        map.put("name", name);
        map.put("roleName",roles.get(0).getRoleName());
        return map;
    }

    public Teacher(Integer roleId, String teacherId, String password, String name, boolean male,
            String phoneNumber, Integer departmentId, String academicTitle) {
        this.roleId = roleId;
        this.teacherId = teacherId;
        this.password = password;
        this.name = name;
        this.male = male;
        this.phoneNumber = phoneNumber;
        this.departmentId = departmentId;
        this.academicTitle = academicTitle;
        this.roles = new LinkedList<Role>();
    }

    public Teacher(String teacherId, String password, String name, boolean male,
            String phoneNumber, Integer departmentId, String academicTitle) {
        this.teacherId = teacherId;
        this.password = password;
        this.name = name;
        this.male = male;
        this.phoneNumber = phoneNumber;
        this.departmentId = departmentId;
        this.academicTitle = academicTitle;
        
    }

    @Override
    public String toString() {
        return "Teacher {" + 
                "academicTitle=" + academicTitle + 
                ", departmentId=" + departmentId + 
                ", male=" + male + 
                ", name=" + name + 
                ", password=" + password + 
                ", phoneNumber=" + phoneNumber + 
                ", roleId=" + roleId + 
                ", roles=" + roles + 
                ", teacherId=" + teacherId + 
                "}";
    }

}
